package pl.soulmusic.phylactery.map;

import java.util.ArrayList;
import java.util.Random;

import pl.soulmusic.phylactery.Phylactery;
import pl.soulmusic.phylactery.db.DAO;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;

public class SongsOverlay extends ItemizedOverlay<Song> {

	// lista obiektów do pokazania
	private ArrayList<Song> items = new ArrayList<Song>();

	// kontekst nadrzędnej aktywności
	private Context ctx = null;

	// lista utworów
	ArrayList<Song> songs = null;

	public SongsOverlay(Drawable marker, Context ctx, ArrayList<Song> songs) {
		super(marker);

		this.ctx = ctx;
		this.songs = songs;

		for (int i = 0; i < this.songs.size(); i++) {
			Song song = songs.get(i);
			items.add(song);
		}
		
		populate();
	}

	@Override
	protected Song createItem(int i) {
		// TODO Auto-generated method stub
		return items.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return items.size();
	}

	/**
	 * Pokazuje odległość od wybranego punktu... coś tu można dodać :)
	 */
	@Override
	protected boolean onTap(final int i) {

		// pobieramy własną pozycję i liczymy odległość do biletomatu
		GeoPoint currentLocation = ((Phylactery) ctx).getMyLocation();
		float[] distance = new float[10];
		Random rand = new Random();
		if (currentLocation == null) currentLocation = new GeoPoint(52409750+rand.nextInt(100000),16923237+rand.nextInt(100000));
		Location.distanceBetween(currentLocation.getLatitudeE6() / 1e6,
				currentLocation.getLongitudeE6() / 1e6, items.get(i).getPoint()
						.getLatitudeE6() / 1e6, items.get(i).getPoint()
						.getLongitudeE6() / 1e6, distance);

		// przygotowujemy tekst
		String text = "Odleglość: " + (int) distance[0] + " [m]";
		text += "\n" + items.get(i).getSnippet()
				+ "\n Last listened: "+ items.get(i).getLoggedTime().format3339(false);
		
        // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);

        // set the message to display
        alertbox.setMessage(text);

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Play", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
            	DAO dao = new DAO(ctx,null);
            	dao.addSongToPlaylist(items.get(i).getId());
                Toast.makeText(ctx, "Added track to playlist", Toast.LENGTH_LONG).show();
            }
        });
        
        // add a neutral button to the alert box and assign a click listener
        alertbox.setNegativeButton("OK", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
            }
        });
        

        // show it
        alertbox.show();

		return true;

	}
}
